package dao.impl;

import dao.IFinancialObjDao;
import entity.FinancialObj;
import entity.User;
import utils.DBconn;

import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class FinancialObjDaoImpl implements IFinancialObjDao {
    @Override
    public boolean insert(FinancialObj financialobj) {
        if (financialobj == null) return false;

        try
        {
            // 初始化连接
            DBconn.init();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            DBconn.addUpdDel(" Insert Into financialobjs(name, count, cost, username, times, date) VALUE(" +
                    "'"+financialobj.getName()+"',"+
                    +financialobj.getCount()+","+
                    +financialobj.getCost()+","+
                    "'"+financialobj.getUsername()+"',"+
                    financialobj.getTimes()+","+
                    "str_to_date('"+sdf.format(new Date())+"', '%Y-%m-%d %H:%i:%s'))"
            );
            DBconn.closeConn();
            return true;
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return false;
    }

    @Override
    public boolean delete(String id) {

        try
        {
            // 初始化连接
            DBconn.init();
            Pattern pattern = Pattern.compile("[0-9]*");
            Matcher isNum = pattern.matcher(id);
            if(isNum.matches()) DBconn.addUpdDel("Delete from financialobjs Where id="+id);
            DBconn.closeConn();
            return true;
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return false;
    }

    @Override
    public boolean delete(FinancialObj financialobj) {
        if (financialobj == null) return false;
        return delete(financialobj.getId());
    }

    @Override
    public boolean update(FinancialObj financialobj) {

        if (financialobj == null) return false;

        try
        {
            // 初始化连接
            DBconn.init();
            DBconn.addUpdDel(" Update financialobjs Set " +
                    "name='"+financialobj.getName()+"',"+
                    "username='"+financialobj.getUsername()+"',"+
                    "count="+financialobj.getCount()+","+
                    "cost="+financialobj.getCost()+","+
                    "times="+financialobj.getTimes()+","+
                    "date=str_to_date('"+financialobj.getDate()+"', '%Y-%m-%d %H:%i:%s')"+
                    " Where id=" + financialobj.getId()
            );

            DBconn.closeConn();
            return true;
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return false;
    }

    @Override
    public List<FinancialObj> getAllFinancialObjInfo(User user) {

        List<FinancialObj> out = new LinkedList<>();

        if (user == null) return out;

        try{
            // 初始化连接
            DBconn.init();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            ResultSet res = DBconn.selectSql("select * from financialobjs where username = '"+ user.getUsername() +"'");
            // 遍历结果集
            while(res.next())
            {
                // 创建并映射属性
                FinancialObj financialobj = new FinancialObj();
                financialobj.setId(String.format("%010d",res.getInt("id")));
                financialobj.setName(res.getString("name"));
                financialobj.setUsername(res.getString("username"));
                financialobj.setCount(res.getInt("count"));
                financialobj.setCost(res.getFloat("cost"));
                financialobj.setTimes(res.getInt("times"));
                financialobj.setDate(sdf.format(res.getDate("date")));
                out.add(financialobj);
            }
            DBconn.closeConn();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return out;
    }
}
